***Last Updated: 11/04/2021 Stata17
/*==========================================*
Paper:			Trade-offs? The Impact of WTO Accession on Intimate Partner Violence in Cambodia

Purpose:        Generate Appendix Table A20 and Appendix Figure A3

To re-run our analysis, please install a folder "Cambodia". There should be 4 subfolders in order for do-files to run:

"created"
"do files"
"graphs"
"output"

/***********************************/
/* INDEX:                          */
/* APPENDIX TABLE A20 Panel A      */
/* APPENDIX FIGURE A3 Panel A      */
/* APPENDIX TABLE A20 Panel B      */
/* APPENDIX FIGURE A3 Panel B      */
/***********************************/

*==========================================*/

clear
clear matrix
clear mata
set more off 
set matsize 2000
set maxvar 32000
cap log close

global dir="XXX\Cambodia"
cd "$dir"

log using "Log_tableA20_figureA3.log", replace



***************************************************************
**PANEL A - APPENDIX TABLE A20 & PANEL A - APPENDIX FIGURE A3
***************************************************************
use "created/DHS_data_additional.dta", clear
ren tariff g

rename ind_share share_emp_ind_bhj_
gen z_ = share_emp_ind_bhj_ * g
rename g g_
reshape wide share_emp_ind_bhj_ g z_, i(district year) j(sector)
egen z = rowtotal(z_*)

global controls="age primaryschool secondaryschool higherschool rural literacy_1-literacy_4 schooling top5_tariff district_1-district_144 year_1-year_2 districttrend1-districttrend144"

tab district, gen(district_)
tab year, gen(year_)
gen trend=0
replace trend=1 if year==2005
replace trend=2 if year==2014
	local i = 1
	while `i'<=145 {
		gen districttrend`i'=district_`i'*trend		
		local i = `i'+1
	}
drop trend

local controls age primaryschool secondaryschool higherschool rural literacy_1-literacy_4 schooling top5_tariff district_1-district_144 year_1-year_2 districttrend1-districttrend144

gen weight=1/145

local weight weight

local y employed 
local x dist_tariff
local z z

local ind_stub share_emp_ind_bhj_
local growth_stub g_

local time_var year
local cluster_var district

levelsof `time_var', local(years)

foreach t of numlist 2000 2005 2014{

	foreach var of varlist `ind_stub'* {
		gen t`t'_`var' = (year == `t') * `var'
		}
	foreach var of varlist `growth_stub'* {
		gen t`t'_`var'b = `var' if year == `t'
		egen t`t'_`var' = max(t`t'_`var'b), by(district)
		drop t`t'_`var'b
		}
	}

tab year, gen(t)
drop t1

foreach var of varlist `ind_stub'* {
	if regexm("`var'", "`ind_stub'(.*)") {
		local ind = regexs(1) 
		}
	tempvar temp
	qui gen `temp' = `var' * `growth_stub'`ind'
	qui regress `x' `temp' `controls' [aweight=`weight'], cluster(district)
	local pi_`ind' = _b[`temp']
	qui test `temp'
	local F_`ind' = r(F)
	qui regress `y' `temp' `controls' [aweight=`weight'], cluster(district)
	local gamma_`ind' = _b[`temp']
	drop `temp'
	}

foreach var of varlist `ind_stub'16 `ind_stub'6 `ind_stub'39 `ind_stub'9 `ind_stub'2 {
	if regexm("`var'", "`ind_stub'(.*)") {
		local ind = regexs(1) 
		}
	tempvar temp
	qui gen `temp' = `var' * `growth_stub'`ind'
	ch_weak, p(.05) beta_range(-10(.1)10)   y(`y') x(`x') z(`temp') weight(`weight') controls(`controls') cluster(district)
	disp r(beta_min) ,  r(beta_max)
	local ci_min_`ind' =string( r(beta_min), "%9.2f")
	local ci_max_`ind' = string( r(beta_max), "%9.2f")
	disp "`ind', `beta_`ind'', `t_`ind'', [`ci_min_`ind'', `ci_max_`ind'']"
	drop `temp'
	}

preserve
keep `ind_stub'* district year `weight'
reshape long `ind_stub', i(district year) j(ind)
gen `ind_stub'pop = `ind_stub'*`weight'
collapse (sd) `ind_stub'sd = `ind_stub' (rawsum) `ind_stub'pop `weight' [aweight = `weight'], by(ind year)
tempfile tmp
save `tmp'
restore

bartik_weight, z(t*_`ind_stub'*)    weightstub(t*_`growth_stub'*) x(`x') y(`y') controls(`controls' ) weight_var(`weight')


mat beta = r(beta)
mat alpha = r(alpha)
mat gamma = r(gam)
mat pi = r(pi)
mat G = r(G)
qui desc t*_`ind_stub'*, varlist
local varlist = r(varlist)


clear
svmat beta
svmat alpha
svmat gamma
svmat pi
svmat G

gen ind = ""
gen year = ""
local t = 1
foreach var in `varlist' {
	if regexm("`var'", "t(.*)_`ind_stub'(.*)") {
		qui replace year = regexs(1) if _n == `t'
		qui replace ind = regexs(2) if _n == `t'
		}
	local t = `t' + 1
	}

/** Calculate Panel C: Variation across years in alpha **/
total alpha1 if year == "2000"
mat b = e(b)
local sum_2000_alpha = string(b[1,1], "%9.3f")
total alpha1 if year == "2005"
mat b = e(b)
local sum_2005_alpha = string(b[1,1], "%9.3f")
total alpha1 if year == "2014"
mat b = e(b)
local sum_2014_alpha = string(b[1,1], "%9.3f")

sum alpha1 if year == "1998"
local mean_1998_alpha = string(r(mean), "%9.3f")
sum alpha1 if year == "2008"
local mean_2008_alpha = string(r(mean), "%9.3f")
sum alpha1 if year == "2014"
local mean_2014_alpha = string(r(mean), "%9.3f")

destring ind, replace
destring year, replace
merge 1:1 ind year using `tmp'
gen beta2 = alpha1 * beta1
gen indshare2 = alpha1 * (`ind_stub'pop/`weight')
gen indshare_sd2 = alpha1 * `ind_stub'sd
gen G2 = alpha1 * G1
collapse (sum) alpha1 beta2 indshare2 indshare_sd2 G2 (mean) G1 , by(ind)
gen agg_beta = beta2 / alpha1
gen agg_indshare = indshare2 / alpha1
gen agg_indshare_sd = indshare_sd2 / alpha1
gen agg_g = G2 / alpha1
rename ind sic
merge 1:1 sic using "created/sic_code_desc.dta"
rename sic ind
gen ind_name = subinstr(description, "Manufacturing n.e.c.", "NEC", .)

gsort -alpha1

capture file close fh
file open fh  using "output/rotemberg_panelA.tex", write replace

/** Panel A: Positive Weights **/
total alpha1 if alpha1 > 0
mat b = e(b)
local sum_pos_alpha = string(b[1,1], "%9.3f")
total alpha1 if alpha1 < 0
mat b = e(b)
local sum_neg_alpha = string(b[1,1], "%9.3f")

sum alpha1 if alpha1 > 0
local mean_pos_alpha = string(r(mean), "%9.3f")
sum alpha1 if alpha1 < 0
local mean_neg_alpha = string(r(mean), "%9.3f")

local share_pos_alpha = string(abs(`sum_pos_alpha')/(abs(`sum_pos_alpha') + 0), "%9.3f")
local share_neg_alpha = string(abs(`sum_neg_alpha')/(abs(`sum_pos_alpha') + abs(`sum_neg_alpha')), "%9.3f")


/** Panel B: Correlations of Industry Aggregates **/
gen F = .
gen agg_pi = .
gen agg_gamma = .
levelsof ind, local(industries)
foreach ind in `industries' {
	capture replace F = `F_`ind'' if ind == `ind'
	capture replace agg_pi = `pi_`ind'' if ind == `ind'
	capture replace agg_gamma = `gamma_`ind'' if ind == `ind'		
	}
corr alpha1 agg_g agg_beta F agg_indshare_sd
mat corr = r(C)
forvalues i =1/5 {
	forvalues j = `i'/5 {
		local c_`i'_`j' = string(corr[`i',`j'], "%9.3f")
		}
	}

/** Panel  D: Top 5 Rotemberg Weight Industries **/
foreach ind in 16 6 39 9 2 {
	qui sum alpha1 if ind == `ind'
   local alpha_`ind' = string(r(mean), "%9.3f")
	qui sum agg_g if ind == `ind'	
	local g_`ind' = string(r(mean), "%9.3f")
	qui sum agg_beta if ind == `ind'	
	local beta_`ind' = string(r(mean), "%9.3f")
	qui sum agg_indshare if ind == `ind'	
	local share_`ind' = string(r(mean)*100, "%9.3f")
	tempvar temp
	qui gen `temp' = ind == `ind'
	gsort -`temp'
	local ind_name_`ind' = ind_name[1]
	drop `temp'
	}


/** Over ID Figure **/
gen omega = alpha1*agg_beta
total omega
mat b = e(b)
local b = b[1,1]

gen label_var = ind 
gen beta_lab = string(agg_beta, "%9.3f")


gen abs_alpha = abs(alpha1) 
gen positive_weight = alpha1 > 0
gen agg_beta_pos = agg_beta if positive_weight == 1
gen agg_beta_neg = agg_beta if positive_weight == 0
twoway (scatter agg_beta_pos agg_beta_neg F if F >= 5 [aweight=abs_alpha ], msymbol(Oh Dh) ), ///
legend(label(1 "Positive Weights") label( 2 "Negative Weights")) yline(`b', lcolor(black) ///
lpattern(dash)) xtitle("First stage F-statistic")  ytitle("{&beta}{subscript:k} estimate") ///
ylabel(0.10(-0.05)-0.35, angle(0) format(%04.2f)) graphregion(color(white)) ///
plotregion(margin(r+10)) title("Panel A: Women's employment", size(4) color(black))
graph save "Graphs/FigureA3_PanelA", replace
*graph export "output/overid_ek_dhs.pdf", replace

/** Panel E: Weighted Betas by alpha weights **/
gen agg_beta_weight = agg_beta * alpha1

collapse (sum) agg_beta_weight alpha1 (mean)  agg_beta, by(positive_weight)
egen total_agg_beta = total(agg_beta_weight)
gen share = agg_beta_weight / total_agg_beta
gsort -positive_weight
local agg_beta_pos = string(agg_beta_weight[1], "%9.3f")
local agg_beta_neg = string(agg_beta_weight[2], "%9.3f")
local agg_beta_pos2 = string(agg_beta[1], "%9.3f")
local agg_beta_neg2 = string(agg_beta[2], "%9.3f")
local agg_beta_pos_share = string(share[1], "%9.3f")
local agg_beta_neg_share = string(share[2], "%9.3f")

/*** Write final table **/
/** Panel A: Women's employment **/
/** I. Correlations **/
file write fh "\multicolumn{5}{l}{\textbf{I. Correlations} }\\" _n
file write fh  " &$\alpha_k$ & \$g_{k}$ & $\beta_k$ & \$F_{k}$ & Var(\$z_k$) \\" _n
file write fh  "\cmidrule(lr){2-6} " _n
file write fh " & \\" _n
file write fh " $\alpha_k$             & 1\\" _n
file write fh " \$g_{k}$                &   `c_1_2'  & 1\\" _n
file write fh " $\beta_{k}$             &   `c_1_3'  & `c_2_3'    &1\\" _n
file write fh " \$F_{k}$                &   `c_1_4'  & `c_2_4'    &  `c_3_4'  & 1\\" _n
file write fh " Var(\$z_{k}$)           &   `c_1_5'  & `c_2_5'    &  `c_3_5'  &  `c_4_5'   &1\\" _n



/** II. Top five Rotemberg weight industries **/
file write fh "\multicolumn{5}{l}{\textbf{II. Top five Rotemberg weight industries} }\\" _n
file write fh  " & $\hat{\alpha}_{k}$ & \$g_{k}$ & $\hat{\beta}_{k}$ & 95 \% CI & \\ \cmidrule(lr){2-6}" _n
foreach ind in 16 6 39 9 2 {
	if `ci_min_`ind'' != -10 & `ci_max_`ind'' != 10 {
		file write fh  "`ind_name_`ind'' & `alpha_`ind'' & `g_`ind'' & `beta_`ind'' & (`ci_min_`ind'',`ci_max_`ind'')  &  \\ " _n
		}
	else  {
		file write fh  "`ind_name_`ind'' & `alpha_`ind'' & `g_`ind'' & `beta_`ind'' & \multicolumn{1}{c}{N/A}  &  \\ " _n
		}
	}

file close fh

***************************************************************
**PANEL B - APPENDIX TABLE A20 & PANEL B - APPENDIX FIGURE A3
***************************************************************
use "created/DHS_data_additional.dta", clear
ren tariff g

rename ind_share share_emp_ind_bhj_
gen z_ = share_emp_ind_bhj_ * g
rename g g_
reshape wide share_emp_ind_bhj_ g z_, i(district year) j(sector)
egen z = rowtotal(z_*)

global controls="age primaryschool secondaryschool higherschool rural literacy_1-literacy_4 schooling top5_tariff district_1-district_144 year_1-year_2 districttrend1-districttrend144"

tab district, gen(district_)
tab year, gen(year_)
gen trend=0
replace trend=1 if year==2005
replace trend=2 if year==2014
	local i = 1
	while `i'<=145 {
		gen districttrend`i'=district_`i'*trend		
		local i = `i'+1
	}
drop trend

local controls age primaryschool secondaryschool higherschool rural literacy_1-literacy_4 schooling top5_tariff district_1-district_144 year_1-year_2 districttrend1-districttrend144

gen weight=1/145

local weight weight

local y physical 
local x dist_tariff
local z z

local ind_stub share_emp_ind_bhj_
local growth_stub g_

local time_var year
local cluster_var district

levelsof `time_var', local(years)

foreach t of numlist 2000 2005 2014{

	foreach var of varlist `ind_stub'* {
		gen t`t'_`var' = (year == `t') * `var'
		}
	foreach var of varlist `growth_stub'* {
		gen t`t'_`var'b = `var' if year == `t'
		egen t`t'_`var' = max(t`t'_`var'b), by(district)
		drop t`t'_`var'b
		}
	}

tab year, gen(t)
drop t1

foreach var of varlist `ind_stub'* {
	if regexm("`var'", "`ind_stub'(.*)") {
		local ind = regexs(1) 
		}
	tempvar temp
	qui gen `temp' = `var' * `growth_stub'`ind'
	qui regress `x' `temp' `controls' [aweight=`weight'], cluster(district)
	local pi_`ind' = _b[`temp']
	qui test `temp'
	local F_`ind' = r(F)
	qui regress `y' `temp' `controls' [aweight=`weight'], cluster(district)
	local gamma_`ind' = _b[`temp']
	drop `temp'
	}

foreach var of varlist `ind_stub'16 `ind_stub'6 `ind_stub'39 `ind_stub'9 `ind_stub'2 {
	if regexm("`var'", "`ind_stub'(.*)") {
		local ind = regexs(1) 
		}
	tempvar temp
	qui gen `temp' = `var' * `growth_stub'`ind'
	ch_weak, p(.05) beta_range(-10(.1)10)   y(`y') x(`x') z(`temp') weight(`weight') controls(`controls') cluster(district)
	disp r(beta_min) ,  r(beta_max)
	local ci_min_`ind' =string( r(beta_min), "%9.2f")
	local ci_max_`ind' = string( r(beta_max), "%9.2f")
	disp "`ind', `beta_`ind'', `t_`ind'', [`ci_min_`ind'', `ci_max_`ind'']"
	drop `temp'
	}

preserve
keep `ind_stub'* district year `weight'
reshape long `ind_stub', i(district year) j(ind)
gen `ind_stub'pop = `ind_stub'*`weight'
collapse (sd) `ind_stub'sd = `ind_stub' (rawsum) `ind_stub'pop `weight' [aweight = `weight'], by(ind year)
tempfile tmp
save `tmp'
restore

bartik_weight, z(t*_`ind_stub'*)    weightstub(t*_`growth_stub'*) x(`x') y(`y') controls(`controls' ) weight_var(`weight')


mat beta = r(beta)
mat alpha = r(alpha)
mat gamma = r(gam)
mat pi = r(pi)
mat G = r(G)
qui desc t*_`ind_stub'*, varlist
local varlist = r(varlist)


clear
svmat beta
svmat alpha
svmat gamma
svmat pi
svmat G

gen ind = ""
gen year = ""
local t = 1
foreach var in `varlist' {
	if regexm("`var'", "t(.*)_`ind_stub'(.*)") {
		qui replace year = regexs(1) if _n == `t'
		qui replace ind = regexs(2) if _n == `t'
		}
	local t = `t' + 1
	}

/** Calculate Panel C: Variation across years in alpha **/
total alpha1 if year == "2000"
mat b = e(b)
local sum_2000_alpha = string(b[1,1], "%9.3f")
total alpha1 if year == "2005"
mat b = e(b)
local sum_2005_alpha = string(b[1,1], "%9.3f")
total alpha1 if year == "2014"
mat b = e(b)
local sum_2014_alpha = string(b[1,1], "%9.3f")

sum alpha1 if year == "1998"
local mean_1998_alpha = string(r(mean), "%9.3f")
sum alpha1 if year == "2008"
local mean_2008_alpha = string(r(mean), "%9.3f")
sum alpha1 if year == "2014"
local mean_2014_alpha = string(r(mean), "%9.3f")

destring ind, replace
destring year, replace
merge 1:1 ind year using `tmp'
gen beta2 = alpha1 * beta1
gen indshare2 = alpha1 * (`ind_stub'pop/`weight')
gen indshare_sd2 = alpha1 * `ind_stub'sd
gen G2 = alpha1 * G1
collapse (sum) alpha1 beta2 indshare2 indshare_sd2 G2 (mean) G1 , by(ind)
gen agg_beta = beta2 / alpha1
gen agg_indshare = indshare2 / alpha1
gen agg_indshare_sd = indshare_sd2 / alpha1
gen agg_g = G2 / alpha1
rename ind sic
merge 1:1 sic using "created/sic_code_desc.dta"
rename sic ind
gen ind_name = subinstr(description, "Manufacturing n.e.c.", "NEC", .)

gsort -alpha1

capture file close fh
*file open fh  using "output/rotemberg_summary_ek_dhs_physical.tex", write replace
file open fh  using "output/rotemberg_panelB.tex", write replace
*file write fh "\toprule" _n

/** Panel A: Positive Weights **/
total alpha1 if alpha1 > 0
mat b = e(b)
local sum_pos_alpha = string(b[1,1], "%9.3f")
total alpha1 if alpha1 < 0
mat b = e(b)
local sum_neg_alpha = string(b[1,1], "%9.3f")

sum alpha1 if alpha1 > 0
local mean_pos_alpha = string(r(mean), "%9.3f")
sum alpha1 if alpha1 < 0
local mean_neg_alpha = string(r(mean), "%9.3f")

local share_pos_alpha = string(abs(`sum_pos_alpha')/(abs(`sum_pos_alpha') + 0), "%9.3f")
local share_neg_alpha = string(abs(`sum_neg_alpha')/(abs(`sum_pos_alpha') + abs(`sum_neg_alpha')), "%9.3f")



/** Panel B: Correlations of Industry Aggregates **/
gen F = .
gen agg_pi = .
gen agg_gamma = .
levelsof ind, local(industries)
foreach ind in `industries' {
	capture replace F = `F_`ind'' if ind == `ind'
	capture replace agg_pi = `pi_`ind'' if ind == `ind'
	capture replace agg_gamma = `gamma_`ind'' if ind == `ind'		
	}
corr alpha1 agg_g agg_beta F agg_indshare_sd
mat corr = r(C)
forvalues i =1/5 {
	forvalues j = `i'/5 {
		local c_`i'_`j' = string(corr[`i',`j'], "%9.3f")
		}
	}

/** Panel  D: Top 5 Rotemberg Weight Industries **/
foreach ind in 16 6 39 9 2 {
	qui sum alpha1 if ind == `ind'
   local alpha_`ind' = string(r(mean), "%9.3f")
	qui sum agg_g if ind == `ind'	
	local g_`ind' = string(r(mean), "%9.3f")
	qui sum agg_beta if ind == `ind'	
	local beta_`ind' = string(r(mean), "%9.3f")
	qui sum agg_indshare if ind == `ind'	
	local share_`ind' = string(r(mean)*100, "%9.3f")
	tempvar temp
	qui gen `temp' = ind == `ind'
	gsort -`temp'
	local ind_name_`ind' = ind_name[1]
	drop `temp'
	}


/** Over ID Figures **/
gen omega = alpha1*agg_beta
total omega
mat b = e(b)
local b = b[1,1]

gen label_var = ind 
gen beta_lab = string(agg_beta, "%9.3f")


gen abs_alpha = abs(alpha1) 
gen positive_weight = alpha1 > 0
gen agg_beta_pos = agg_beta if positive_weight == 1
gen agg_beta_neg = agg_beta if positive_weight == 0
twoway (scatter agg_beta_pos agg_beta_neg F if F >= 5 [aweight=abs_alpha ], msymbol(Oh Dh) ), legend(label(1 "Positive Weights") label( 2 "Negative Weights")) yline(`b', lcolor(black) lpattern(dash)) xtitle("First stage F-statistic")  ytitle("{&beta}{subscript:k} estimate") graphregion(color(white)) ///
ylabel(0.10(-0.02)-0.10, angle(0) format(%04.2f)) ///
plotregion(margin(r+10)) title("Panel B: Physical violence", size(4) color(black))
graph save "graphs/FigureA3_PanelB", replace

graph combine "graphs/FigureA3_PanelA.gph" "graphs/FigureA3_PanelB", col(1) graphregion(color(white) lcolor(white) lwidth(thick)) ///
	ysize(9) xsize(6) title("", size(3.5) color(black)) saving("graphs/FigureA3.gph", replace)

graph export "graphs/FigureA3.eps", replace font($grfont)
*graph export "output/overid_ek_dhs_physical.pdf", replace


/** Panel E: Weighted Betas by alpha weights **/
gen agg_beta_weight = agg_beta * alpha1

collapse (sum) agg_beta_weight alpha1 (mean)  agg_beta, by(positive_weight)
egen total_agg_beta = total(agg_beta_weight)
gen share = agg_beta_weight / total_agg_beta
gsort -positive_weight
local agg_beta_pos = string(agg_beta_weight[1], "%9.3f")
local agg_beta_neg = string(agg_beta_weight[2], "%9.3f")
local agg_beta_pos2 = string(agg_beta[1], "%9.3f")
local agg_beta_neg2 = string(agg_beta[2], "%9.3f")
local agg_beta_pos_share = string(share[1], "%9.3f")
local agg_beta_neg_share = string(share[2], "%9.3f")

/*** Write final table **/
/** Panel B: Physical violence **/
/** I. Correlations **/
file write fh "\multicolumn{5}{l}{\textbf{I. Correlations} }\\" _n
file write fh  " &$\alpha_k$ & \$g_{k}$ & $\beta_k$ & \$F_{k}$ & Var(\$z_k$) \\" _n
file write fh  "\cmidrule(lr){2-6} " _n
file write fh " & \\" _n
file write fh " $\alpha_k$             & 1\\" _n
file write fh " \$g_{k}$                &   `c_1_2'  & 1\\" _n
file write fh " $\beta_{k}$             &   `c_1_3'  & `c_2_3'    &1\\" _n
file write fh " \$F_{k}$                &   `c_1_4'  & `c_2_4'    &  `c_3_4'  & 1\\" _n
file write fh " Var(\$z_{k}$)           &   `c_1_5'  & `c_2_5'    &  `c_3_5'  &  `c_4_5'   &1\\" _n

/** II. Top five Rotemberg weight industries **/
file write fh "\multicolumn{5}{l}{\textbf{II. Top five Rotemberg weight industries} }\\" _n
file write fh  " & $\hat{\alpha}_{k}$ & \$g_{k}$ & $\hat{\beta}_{k}$ & 95 \% CI &  \\ \cmidrule(lr){2-6}" _n
foreach ind in 16 6 39 9 2 {
	if `ci_min_`ind'' != -10 & `ci_max_`ind'' != 10 {
		file write fh  "`ind_name_`ind'' & `alpha_`ind'' & `g_`ind'' & `beta_`ind'' & (`ci_min_`ind'',`ci_max_`ind'')  &  \\ " _n
		}
	else  {
		file write fh  "`ind_name_`ind'' & `alpha_`ind'' & `g_`ind'' & `beta_`ind'' & \multicolumn{1}{c}{N/A}  &  \\ " _n
		}
	}

file close fh


log close


